package vn.csc.javafresher.xml.finalassignment.servlet;

import java.io.File;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import vn.csc.javafresher.xml.finalassignment.entity.Guest;
import vn.csc.javafresher.xml.finalassignment.service.XMLService;

public class ShowGuestListServlet extends HttpServlet{
	private static final long serialVersionUID = 2397162347277371675L;
	private static String CONTEXT_PATH = "";
	
	@Override
	public void init(ServletConfig config) throws ServletException {
		CONTEXT_PATH = config.getServletContext().getRealPath("").toString();
	}
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException {
		try {
			XMLService xmlService = new XMLService(new File(CONTEXT_PATH + "\\WEB-INF\\guest.xml"));
			String result = "<table border='1'><tr><td>Name</td><td>Email</td></tr>";
			for (Guest guest : xmlService.getGuestList().guest) {
				if("yes".equalsIgnoreCase(guest.subscribe)) {
					result += "<tr>";
					result += "<td>" + guest.name + "</td><td>" + guest.email + "</td>";
					result += "</tr>";
				}
			}
			result += "</table><a href=\"index.jsp\">Back</a>";
			PrintWriter out = response.getWriter();
			out.println(result);
		} catch (Exception e) {
			System.err.print(e.getMessage().toString());
		}
	}
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException {
		
	}
}
